﻿using AutoMapper;
using YDT_SeckillService.Applictions.DTOs;
using YDT_SeckillService.Domain.Entities;
using YDT_SeckillService.Domain.Enums;

namespace YDT_SeckillService.Applictions.Mappings
{
    /// <summary>
    /// AutoMapper映射配置 - 定义DTO和实体之间的映射规则
    /// </summary>
    public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            // 秒杀活动映射配置
            CreateMap<Seckill, SeckillDto>()
                .ForMember(dest => dest.TimeStatus,
                           opt => opt.MapFrom(src => src.TimeStatus.ToString()))
                .ForMember(dest => dest.SeckillDate,
                           opt => opt.MapFrom(src => src.SeckillDate.ToString("yyyy/MM/dd")))
                .ForMember(dest => dest.SeckillStartTime,
                           opt => opt.MapFrom(src => src.SeckillStartTime.ToString("HH:mm")))
                .ForMember(dest => dest.SeckillEndTime,
                           opt => opt.MapFrom(src => src.SeckillEndTime.ToString("HH:mm")))
                .ForMember(dest => dest.ActivityStatus,
                           opt => opt.MapFrom(src => GetActivityStatus(src)));

            // 秒杀商品映射配置
            CreateMap<SeckillProduct, SeckillProductDto>()
                .ForMember(dest => dest.SeckillType,
                           opt => opt.MapFrom(src => src.SeckillType.ToString()))
                .ForMember(dest => dest.SeckillStatus,
                           opt => opt.MapFrom(src => src.SeckillStatus.ToString()));

            // 创建秒杀活动映射配置
            CreateMap<CreateSeckillDto, Seckill>();

            // 更新秒杀活动映射配置
            CreateMap<UpdateSeckillDto, Seckill>()
                .ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));

            // 创建秒杀商品映射配置
            CreateMap<CreateSeckillProductDto, SeckillProduct>();

            // 更新秒杀商品映射配置
            CreateMap<UpdateSeckillProductDto, SeckillProduct>()
                .ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));
        }

        /// <summary>
        /// 获取活动状态
        /// </summary>
        private static string GetActivityStatus(Seckill seckill)
        {
            if (seckill.TimeStatus == SeckillTimeStatus.Disabled)
                return "已禁用";

            if (seckill.IsActive())
                return "进行中";

            if (seckill.IsUpcoming())
                return "即将开始";

            if (seckill.IsExpired())
                return "已结束";

            return "未知";
        }
    }
}
